/*
 * @Date: 2024-11-05 10:28:14
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2024-11-11 14:03:03
 * @Description: 下级项目完善审核
 */

import XEUtils from 'xe-utils';
import HandlerUtils from '@/handler/utils/HandlerUtils.js'

import { reactive } from "vue"
import { useProjectApi } from '@/api/index'

import { RkConfirmBox } from '@/components/message-box/index.js'
import { openOperationLogs } from '@/plugins/call-comp/CallCompService.js'

import WfTask from '@/handler/pojo/WfTask.js'

const useLayoutHandler = (params) => {
  // 参数
  const { routeQuery, userInfo, tableSearch } = params

  // 基本信息
  const baseData = reactive({
    // 选中项目ID
    proId: '',
    // 选中区划编码
    adCode: userInfo.adBjCode,
    // 单位ID
    agId: '',
    // 选中单位编码
    agCode: '',
    // 单位名称
    agName: '',
    // 上级单位Id
    agParentId: '',
    // 工作流
    processKey: routeQuery.processKey,
  })
  /**
   * 表单渲染业务按钮点击事件
   */
  const btnGroupRightClick = async ({ rightItem, extParams }) => {
    const { agData, checkRows } = extParams
    const btnType = rightItem.value
    if (btnType === 'send') {
      HandlerUtils.openLoadService(rightItem.label)
      const reqParams = coverOperateParams(checkRows, WfTask.SEND.value)
      const result = await useProjectApi().improveProHandleTask(reqParams)
      if (result.code === 200) {
        HandlerUtils.successMsgTip(rightItem.label)
        tableSearch()
      } else {
        HandlerUtils.failedMsgTip(result.msg, true)
      }
      HandlerUtils.closeLoadService()
    } else if (btnType === 'cancel') {
      RkConfirmBox({
        title: '提示',
        content: `您确定要${rightItem.label}吗？`,
        onSubmit: async () => {
          HandlerUtils.openLoadService(rightItem.label)
          const reqParams = coverOperateParams(checkRows, WfTask.CANCEL.value)
          const result = await useProjectApi().improveProHandleTask(reqParams)
          if (result.code === 200) {
            HandlerUtils.successMsgTip(rightItem.label)
            tableSearch()
          } else {
            HandlerUtils.failedMsgTip(result.msg, true)
          }
          HandlerUtils.closeLoadService()
        }
      })
    } else if (btnType === 'delete') {
      RkConfirmBox({
        title: '提示',
        content: `您确定要${rightItem.label}吗？`,
        onSubmit: async function () {
          HandlerUtils.openLoadService(rightItem.label)
          const reqParams = coverOperateParams(checkRows, WfTask.CANCEL.value)
          const result = await useProjectApi().deleteProResPerfect({
            perfectIds: reqParams.perfectIds
          })
          if (result.code === 200) {
            HandlerUtils.successMsgTip(rightItem.label)
            tableSearch()
          } else {
            HandlerUtils.failedMsgTip(result.msg, true)
          }
          HandlerUtils.closeLoadService()
        }
      })
    } else if (btnType === 'log') {
      openOperationLogs(checkRows)
    }
  }
  /**
   * 
   * @param {Object} btnItem 
   * @param {*} checkRows 
   */
  const onTableButtonsClick = (btnItem, checkRows) => {
    if (btnItem.value === 'logs') {
      if (!checkRows.length) {
        HandlerUtils.warningMsgTip('请至少选择一条数据进行操作！')
        return
      }
    } else if (btnItem.value === 'back') {
      if (!checkRows.length) {
        HandlerUtils.warningMsgTip('请至少选择一条数据进行操作！')
        return
      }
    } else if (btnItem.value === 'send') {
      if (!checkRows.length) {
        HandlerUtils.warningMsgTip('请至少选择一条数据进行操作！')
        return
      }
    } else if (btnItem.value === 'cancel') {
      if (!checkRows.length) {
        HandlerUtils.warningMsgTip('请至少选择一条数据进行操作！')
        return
      }
    }
    console.log('onTableButtonsClick', { btnItem, checkRows })
  }


  /**
   * 封装操作数据
   * 
   * @param checkRows 选中的数据
   * @param wfType 工作流状态
   */
  const coverOperateParams = (checkRows, wfType) => {
    let proIds = []
    let taskIds = []
    let applyIds = []
    let perfectIds = []
    for (const row of checkRows) {
      proIds.push(row.proId)
      taskIds.push(row.taskId)
      applyIds.push(row.applyId)
      perfectIds.push(row.perfectId)
    }
    return {
      // 流程节点
      nodeId: routeQuery.nodeId,
      proIds: proIds.join(','),
      taskIds: taskIds.join(','),
      applyIds: applyIds.join(','),
      perfectIds: perfectIds.join(','),
      taskType: wfType || '',
      adCode: baseData.adCode,
      agCode: '',
      processKey: baseData.processKey,
      bondTypeCode: 2,
    }
  }

  return {
    baseData,
    buttonClick: null,
    btnGroupRightClick,
    onTableButtonsClick,
  }
}

export default useLayoutHandler
